<template>
  <div class="uploadimg">
    <el-upload
      v-if="!imgUrl"
      action="https://jsonplaceholder.typicode.com/posts/"
      list-type="picture-card"
      :before-upload="beforeUpload"
      :http-request="httprequest"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <div v-else class="prepic">
      <img :src="imgUrl" v-errimg="baseImg" class="myimg" @click="preview" />
      <i class="el-icon-close" @click="delpre"> </i>
    </div>
    <el-progress
      v-if="progerssshow"
      style="width:150px"
      :show-text="false"
      :percentage="percentage"
    ></el-progress>
    <el-dialog title="图片预览" :visible.sync="showDialog">
      <div class="preshow">
        <img :src="imgUrl" alt="" class="myimg" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: ['imgUrl'],
  data () {
    return {
      showDialog: false,
      cos: null,
      progerssshow: false,
      percentage: 0,
      baseImg: require('../../assets/common/bigUserHeader.png')
    }
  },
  methods: {
    preview () {
      this.showDialog = true
    },
    delpre () {
      this.$emit('update:imgUrl', '')
    },
    beforeUpload (file) {
      const isSize = file.size / 1024 / 1024 < 2
      if (!isSize) {
        this.$message.warning('上传图片大小不得的大于2M')
      }
      const isType =
        file.type === 'image/jpeg' ||
        file.type === 'image/jpg' ||
        file.type === 'image/png'
      if (!isType) {
        this.$message.warning('上传图片格式不正确')
      }
      return isType && isSize
    },
    httprequest (file) {
      this.progerssshow = true
      var myfile = file.file
      console.log(myfile)
      this.cos.putObject(
        {
          Bucket: 'harz-1306481886-1306481886' /* 必须 */,
          Region: 'ap-shenzhen-fsi' /* 存储桶所在地域，必须字段 */,
          Key: myfile.name /* 必须 */,
          StorageClass: 'STANDARD',
          Body: myfile, // 上传文件对象
          onProgress: progressData => {
            // progressData: 上传过程中的实时进度对象
            console.log(progressData)
            this.percentage = progressData.percent * 100
          }
        },
        (err, data) => {
          // data: 上传成功后的返回值
          // err: 上传出错的错误信息
          if (err) {
            this.$message.error(err.message)
            return
          }
          // 将返回的图片地址赋值给 imgUrl
          this.$emit('update:imgUrl', 'http://' + data.Location)
          setTimeout(() => {
            // 关闭进度条
            this.progerssshow = false
            this.$emit('update:imgUrl', '')
          }, 1000)
        }
      )
    }
  },
  created () {
    this.cos = new COS({
      SecretId: 'AKIDlLglZ6ML3Zppb8QSFo9F6i4A0cz6ybz7',
      SecretKey: 'tc8PRFp9D1g9MJ2VigOr65oGRLxMVNyH'
    })
  }
}
</script>

<style lang="scss" scoped>
.uploadimg {
  padding: 20px;
  .prepic {
    width: 150px;
    height: 150px;
    position: relative;
    i {
      position: absolute;
      top: 0;
      right: 0;
      background-color: rgba(56, 48, 48, 0.5);
      border-radius: 0 0 0 10px;
      display: none;
    }
    &:hover i {
      display: block;
      cursor: pointer;
    }
    .myimg {
      width: 150px;
      height: 150px;
    }
  }
  .preshow {
    text-align: center;
    padding-bottom: 100px;
    padding-top: 80px;
    .myimg {
      width: 250px;
      height: 250px;
    }
  }
}
</style>
